﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Lucy.Engine.Framework.Components
{
    public class MoveComponent : Component
    {
        public float Speed { get; set; }

        public MoveComponent()
        {
            Speed = 200;
        }

        public override IEnumerable<string> GetProperties()
        {
            yield return "Speed";
        }

        public override void SetValue(string propertyName, string value)
        {
            switch (propertyName)
            {
                case "Speed":
                    Speed = float.Parse(value);
                    break;
            }
        }

        public override string GetValue(string propertyName)
        {
            switch (propertyName)
            {
                case "Speed":
                    return Speed.ToString();
            }

            return string.Empty;
        }

        public override void OnUpdate(float elapsed, GameObject gameObject)
        {
            KeyboardState currentState = Keyboard.GetState();

            Vector2 add = Vector2.Zero;
            if (currentState.IsKeyDown(Keys.Left))
                add = -Vector2.UnitX * Speed * elapsed;
            else if (currentState.IsKeyDown(Keys.Right))
                add = Vector2.UnitX * Speed * elapsed;

            if (currentState.IsKeyDown(Keys.Up))
                add = -Vector2.UnitY * Speed * elapsed;
            else if (currentState.IsKeyDown(Keys.Down))
                add = Vector2.UnitY * Speed * elapsed;

            gameObject.Position = new Vector2(gameObject.Position.X, gameObject.Position.Y) + add;
        }

        public override void OnRender(SpriteBatch spriteBatch, GameObject gameObject)
        {
        }
    }
}
